<template>
    <div>
        <ModalComponent @open="handleOpen" modal-name="grading-teacher-info" :title="(type == 'add' ? '新增' : '编辑') + '老师'"
            :close-on-click-modal="false" size="1024px">
            <div>
                <el-form :model="form" label-width="120px" size="large">
                    <template v-if="type == 'add'">
                        <el-form-item label="老师数量">
                            <el-input v-model="form.row" type="number" placeholder="请输入新增老师数量" />
                        </el-form-item>
                    </template>
                    <template v-if="type == 'edit'">
                        <el-form-item label="老师名称">
                            <el-input v-model="form.teacherName" placeholder="请输入老师名称" />
                        </el-form-item>
                        <el-form-item label="账号">
                            <el-input v-model="form.teacherUsername" />
                        </el-form-item>
                        <el-form-item label="密码">
                            <el-input v-model="form.teacherPassword" />
                        </el-form-item>
                    </template>

                    <el-form-item label="">
                        <el-button type="primary" @click="handleSave">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </ModalComponent>
    </div>
</template>
<script setup >
import { onMounted, ref } from 'vue'
import ModalComponent from "@/components/modal/index.vue"
import { AddGradingTeacher, UpdateGradingTeacher } from "@/api/gradingTeacher"
import { ElMessage } from 'element-plus';
const form = ref({});
const type = ref("add")
const emit = defineEmits(['update'])
const handleOpen = (e) => {
    type.value = e.type;
    if (e.type == 'add') {
        form.value = {
            siteId: e.data.siteId,
            row: 1
        }
    } else if (e.type == 'edit') {
        form.value = {
            siteId: e.data.siteId,
            gradingTeacherId: e.data.gradingTeacherId,
            teacherName: e.data.teacherName,
            teacherUsername: e.data.teacherUsername,
            teacherPassword: e.data.teacherPassword,
        }
    }
}
const handleSave = async () => {
    // TODO 验证
    let res
    if (type.value == 'add') {
        res = await AddGradingTeacher({
            row: form.value.row,
            siteId: form.value.siteId
        })
    } else if (type.value == 'edit') {
        res = await UpdateGradingTeacher(form.value)
    }

    if (!res.success) {
        ElMessage.error(res.message);
    } else {
        ElMessage.success(res.message);
        emit('update')
    }

}
onMounted(() => {
    console.log('mounted!')
})
</script>
<style lang="scss" scoped></style>